﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BoriPeter.FitnessJournal8.Models
{
    public class DailyEntry : ModelBase, IIdentifiable, IMergeable<DailyEntry>
    {
        #region Date
        private DateTime date;
        public DateTime Date
        {
            get
            {
                return this.date;
            }

            set
            {
                this.date = value;
                NotifyOfPropertyChange("Date");
            }
        }
        #endregion

        #region WeightKg
        private decimal weightKg;
        public decimal WeightKg
        {
            get
            {
                return this.weightKg;
            }

            set
            {
                this.weightKg = value;
                NotifyOfPropertyChange("WeightKg");
            }
        }
        #endregion

        #region WeightComment
        private string weightComment;
        public string WeightComment
        {
            get
            {
                return this.weightComment;
            }

            set
            {
                this.weightComment = value;
                NotifyOfPropertyChange("WeightComment");
            }
        }
        #endregion

        #region CaloriesIntake
        /// <summary>
        /// calories is a Decimal – the total daily energy consumed in kcal
        /// </summary>
        private decimal caloriesIntakeKcal;
        public decimal CaloriesIntakeKcal
        {
            get
            {
                return this.caloriesIntakeKcal;
            }

            set
            {
                this.caloriesIntakeKcal = value;
                NotifyOfPropertyChange("CaloriesIntakeKcal");
                CaloriesBalance = caloriesIntakeKcal - caloriesBurnedKcal;
            }
        }
        #endregion

        #region CaloriesBurned
        private decimal caloriesBurnedKcal;
        public decimal CaloriesBurnedKcal
        {
            get
            {
                return this.caloriesBurnedKcal;
            }

            set
            {
                this.caloriesBurnedKcal = value;
                NotifyOfPropertyChange("CaloriesBurnedKcal");
                CaloriesBalance = caloriesIntakeKcal - caloriesBurnedKcal;
            }
        }
        #endregion

        #region CaloriesBalance
        private decimal caloriesBalance;
        public decimal CaloriesBalance
        {
            get
            {
                return this.caloriesBalance;
            }

            private set
            {
                this.caloriesBalance = value;
                NotifyOfPropertyChange("CaloriesBalance");
            }
        }
        #endregion

        #region Carbohydrate
        /// <summary>
        /// carbohydrate is a Decimal – the total daily carbohydrate consumed in grams.
        /// </summary>
        private decimal carbohydrateGram;
        public decimal CarbohydrateGram
        {
            get
            {
                return this.carbohydrateGram;
            }

            set
            {
                this.carbohydrateGram = value;
                NotifyOfPropertyChange("CarbohydrateGram");
                UpdateCalorieBreakdown();
            }
        }
        #endregion

        #region Protein
        /// <summary>
        /// protein is a Decimal – the total daily protein consumed in grams.
        /// </summary>
        private decimal proteinGram;
        public decimal ProteinGram
        {
            get
            {
                return this.proteinGram;
            }

            set
            {
                this.proteinGram = value;
                NotifyOfPropertyChange("ProteinGram");
                UpdateCalorieBreakdown();
            }
        }
        #endregion

        #region Fat
        /// <summary>
        /// fat is a Decimal – the total daily total fat consumed in grams.
        /// </summary>
        private decimal fatGram;
        public decimal FatGram
        {
            get
            {
                return this.fatGram;
            }

            set
            {
                this.fatGram = value;
                NotifyOfPropertyChange("FatGram");
                UpdateCalorieBreakdown();
            }
        }
        #endregion

        #region FoodEntries
        private ObservableCollection<FoodEntry> foodEntries;
        public ObservableCollection<FoodEntry> FoodEntries
        {
            get
            {
                return this.foodEntries;
            }

            set
            {
                this.foodEntries = value;
                NotifyOfPropertyChange("FoodEntries");
            }
        }
        #endregion

        #region ExerciseEntries
        private ObservableCollection<ExerciseEntry> exerciseEntries;
        public ObservableCollection<ExerciseEntry> ExerciseEntries
        {
            get
            {
                return this.exerciseEntries;
            }

            set
            {
                this.exerciseEntries = value;
                NotifyOfPropertyChange("ExerciseEntries");
            }
        }
        #endregion

        #region CalorieBreakdown
        private ObservableCollection<NameValuePair> calorieBreakdown;
        public ObservableCollection<NameValuePair> CalorieBreakdown
        {
            get
            {
                if (calorieBreakdown == null)
                    UpdateCalorieBreakdown();
                return this.calorieBreakdown;
            }

            set
            {
                this.calorieBreakdown = value;
                NotifyOfPropertyChange("CalorieBreakdown");
            }
        }
        #endregion

        #region UpdateCalorieBreakdown()
        private void UpdateCalorieBreakdown()
        {
            var cb = new ObservableCollection<NameValuePair>();

            if (CarbohydrateGram != 0)
                cb.Add(new NameValuePair() { Name = CARBOHYDRATE, Value = float.Parse(CarbohydrateGram.ToString()) });

            if (FatGram != 0)
                cb.Add(new NameValuePair() { Name = FAT, Value = float.Parse(FatGram.ToString()) });

            if (ProteinGram != 0)
                cb.Add(new NameValuePair() { Name = PROTEIN, Value = float.Parse(ProteinGram.ToString()) });

            CalorieBreakdown = cb;
        }
        #endregion

        #region Merge()
        public DailyEntry Merge(DailyEntry _entry)
        {
            return MergeObjects.Merge<DailyEntry>(this, _entry);
        }
        #endregion

        #region GetIdentifier()
        public int GetIdentifier()
        {
            TimeSpan SinceEpoch = date - new DateTime(1970, 1, 1);
            var totalDays = Math.Round(SinceEpoch.TotalDays);
            int id = (int)totalDays;

            return id;
        }
        #endregion
    }
}
